import { Component, OnInit } from '@angular/core';
import {Router, ActivatedRoute} from "@angular/router";
import {PageSubject} from "../../../../util/page-subject";
import {OneDialogService} from "../../../../widgets/one-dialog/one-dialog.service";
import {AccountsService} from "../accounts.service";
import {ResponseUtil} from "../../../../util/response-util";
import {Path} from "../../../../portal/admin/admin-breadcrumb/breadcrumb";
import {MsgResult} from "../../../../models/msg-result";
import {MsgConstants} from "../../../../constants/constants";
import {TreeUtil} from "../../../../util/tree-util";
import {LogUtil} from "../../../../util/log-util";

declare var $;

@Component({
  selector: 'app-account-set-menu',
  templateUrl: './account-set-menu.component.html'
})
export class AccountSetMenuComponent implements OnInit {

  uri :string;

  menuId :string;

  constructor(
    private router: Router,
    private route: ActivatedRoute,
    private pageSubject: PageSubject,
    private dialogService: OneDialogService,
    private accountsService: AccountsService,
    private responseUtil: ResponseUtil,
    private logUtil: LogUtil,
    private treeUtil: TreeUtil,
  ) { }

  ngOnInit() {
    this.pushPage();
    this.getMenu();
  }
  /**
   * 面包屑导航
   */
  private pushPage() {
    let path = new Path("权限系统", "");
    this.pageSubject.pushPage("账户管理", "设置首页", path);
  }

  getMenu(){
    this.accountsService.getMenusForAccount().subscribe(
      result => this.responseUtil.success(result, this.processMenuTreeResult),
      message => this.responseUtil.error(message)
    );
  }

  /**
   * 获取初始化权限树日志
   * @param result
   */
  private processMenuTreeResult = (result: MsgResult) => {
    if (result && result.code == MsgConstants.MSG_CODE_OK) {
     let menusArr = result.data || [];

     menusArr.forEach(function (menu) {
       if(!(menu.isExternal == '0' && menu.isExistUrl == '1')){
          menu.nocheck = true;
       }
     });

     this.initMenuZTree(menusArr);
    }
    if (result && result.code != MsgConstants.MSG_CODE_OK) {
      this.dialogService.info(result.message);
    }
  }




  /**
   *  菜单树处理
   * @param menus
   * @param authorityConfigId  :,authorityConfigId
   */
  initMenuZTree(AuthorityList) {

    let zNodes = AuthorityList;

    let setting = {
      data: {
        simpleData: {
          enable: true
        },
        keep: {
          parent: true,
          leaf: true
        },
        key:{
          title : "",
          url : "xurl"
        }
      },
      check: {
        enable: true,
        chkStyle: "radio",
        radioType:"all",
        nocheckInherit: false
      },
      callback: {
        onCheck: null,
        onClick: null
      }


    };
    //把树放进页面
    this.treeUtil.init("account_allot_menu_tree",setting, zNodes)
  }

  /**
   * 处理勾中复选框数据
   * 分配首页的菜单id：14
   * 分配首页的菜单uri：admin/permi/roles;mid=14
   */
  processingSelectedData() {
    let treeObj = $.fn.zTree.getZTreeObj("account_allot_menu_tree");
    let nodes = treeObj.getCheckedNodes(true);
    if(nodes.length>0){
      this.menuId = nodes[0].id
      this.uri = nodes[0].url+';mid='+ this.menuId;
      this.logUtil.info("分配首页的菜单uri："+this.uri);
    }
  }

  /**
   * 保存分配给账户的菜单
   */
  saveAssignMenu(){
    this.processingSelectedData();
      let accountIdArr = this.route.snapshot.params["accountId"];
      this.logUtil.info("批量分配的首页账户："+accountIdArr);
    if (!!accountIdArr) {
      this.accountsService.settingsHomePage(accountIdArr, this.uri, this.menuId).subscribe(
        result => this.responseUtil.success(result, this.processAssignMenuResult),
        message => this.responseUtil.error(message)
      );
    }
  }

  processAssignMenuResult = (result: MsgResult) => {
    if (result && result.code == MsgConstants.MSG_CODE_OK) {
      this.dialogService.modalSuccess(result.message ,()=>this.cancelAssignMenu());
    }
    if (result && result.code != MsgConstants.MSG_CODE_OK) {
      this.dialogService.modalInfo(result.message ,()=>this.cancelAssignMenu());
    }
  }


  /**
   * 取消修改
   */
  cancelAssignMenu() {
    this.router.navigate(['..'], {relativeTo: this.route});
  }

}
